import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
import Home from '../views/Home/index.vue'
import Reg from "../views/Reg/Index.vue";
import Regsms from "../views/Reg/Sms.vue";
import Regpass from "../views/Reg/Pass.vue";
import myindex from "../views/User/index.vue"
import gooddetail from "../views/Gooddetal/index.vue"
import shoppingcart from "../views/ShoppingCart/index.vue"
import Login from "../views/Login/index.vue"
import NotFound from "../views/NotFound.vue"
// import Classify from "../views/Classify/index.vue"



// 创建路由对象
const router = createRouter({

    history: createWebHashHistory(),

    routes: [
        {
            path: '/',
            component: Home
        },
        {
            path: "/Home",
            component: Home,
        },
        {   
            path:"/mysearch",
            name:'mysearch',
            component:()=>import("../views/Home/components/MySearch.vue")
            // component:()=>import("../views/Classify/index.vue")
        },
        {
            path: '/myindex',
            component: myindex,
            meta: {
                requireAuth: true
            }
        },
        {
            name:"gooddetail",
            path: '/gooddetail/:id',
            component: gooddetail
        },
        {
            name: "Reg",
            path: "/Reg",
            component: Reg
        },
        {
            name: "Regsms",
            path: "/Regsms/:tel",
            component: Regsms
        },
        {
            name: "Regpass",
            path: "/Regpass",
            component: Regpass
        },
        {
            path: "/shoppingcart",
            component: shoppingcart,
            meta: {
                requireAuth: true
            }
        },
        {
            name: "Login",
            path: "/Login",
            component: Login
        },
        { 
            // path:'/user-:afterUser(.*)*',
            path:'/:pathMatch(.*)*',
            component:NotFound
        },
        {
            path:"/Classify",
            // component:Classify
            component:()=>import("../views/Classify/index.vue")
        },
        {
            name:"Address",
            path:"/Address",
            component:()=>import("../views/City/address.vue")
        },
        {
            name:"list",
            path:"/list",
            component:()=>import("../views/List/list.vue")
        },
        {
            name:"confirmorder",
            path:"/confirmorder",
            component:()=>import("../views/ConfirmOrder/confirmorder.vue")
        },
        {
            name:"myaddress",
            path:"/myaddress",
            component:()=>import("../views/address/AddRess.vue")
        },
        {
            name:"NewAddress",
            path:"/NewAddress",
            component:()=>import("../views/NewAddress/NewAddress.vue")
        },
        {
            name:"ChangeAdd",
            path:"/ChangeAdd",
            component:()=>import("../views/NewAddress/ChangeAdd.vue")
        },
        {
            name:"copy",
            path:"/copy",
            component:()=>import("../views/Copy/copy.vue")
        }
    ]
})


router.beforeEach((to, from, next) => {

    if (to.meta.requireAuth) {
        // console.log(to.meta.requireAuth);
        if (!localStorage.getItem("userid")) {
            next("/Login?path=" + to.path);
        } else {
            next(true);
        }
    }else{
        next(true);//继续前行。
    }
    
})

export default router